<?php

namespace app\models;

use think\Model;

/**
 * Class CourseModel.
 */
class CourseModel extends Model
{
    protected $table = 'edu_course';

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|CourseModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $course = self::get(['id' => $id]);
        if ($course) {
            return (isset($course[$type])) ? $course[$type] : $course;
        }

        return [];
    }

    /**
     * 获取课程列表.
     *
     * @param $param
     *
     * @throws \think\Exception
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getCourseManageLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['course_name']) && '' !== $param['course_name']) {
            $where['course_name'] = ['like', '%'.$param['course_name'].'%'];
        }

        if (isset($param['student_grade']) && '' !== $param['student_grade']) {
            $where['student_grade'] = $param['student_grade'];
        }

        if (isset($param['subject']) && '' !== $param['subject']) {
            $where['subject'] = $param['subject'];
        }

        if (isset($param['course_type']) && '' !== $param['course_type']) {
            $where['course_type'] = $param['course_type'];
        }


        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }

        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = $param['campus_id'];
        }else {
            if (get_current_type() == 4 || get_current_type() == 5) {
                $where['campus_id'] = get_current_campus_id();
            } elseif (get_current_type() == 2 || get_current_type() == 3) {
                $where['campus_id'] = ['in', get_current_campus_id()];
            }
        }



        $listData = $this->where($where)->order('update_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['price'] = $item['price'] / 100;
                $item['student_grade'] = DictionaryModel::getById($item['student_grade'], 'name');
                $item['student_grade'] = $item['student_grade'] ? $item['student_grade'] : '-';
                $item['subject'] = DictionaryModel::getById($item['subject'], 'name');
                $item['subject'] = $item['subject'] ? $item['subject'] : '-';
                $item['course_type'] = DictionaryModel::getById($item['course_type'], 'name');
                $item['course_type'] = $item['course_type'] ? $item['course_type'] : '-';
                $item['class_type'] = DictionaryModel::getById($item['class_type'], 'name');
                $item['class_type'] = $item['class_type'] ? $item['class_type'] : '-';
                $item['cost_type'] = DictionaryModel::getById($item['cost_type'], 'name');
                $item['cost_type'] = $item['cost_type'] ? $item['cost_type'] : '-';
                $campus_num = CampusPriceConfigureModel::getByIdCampusNum($item['id']);
                $item['campus_num'] = $campus_num.'校区';
                if($item['cost_type'] == '按期收费' ||$item['cost_type'] == 97){
                    $item['stage_charge'] = $item['class_num'].'节'.'/'. $item['unit'];
                }else{
                    $item['stage_charge'] = '-';
                }
                if($item['cost_type'] == '按期收费' ||$item['cost_type'] == 97){

                    $item['price_amount'] = ($item['price'] * $item['class_num']) . '/'.$item['unit'];

                }else{
                    $item['price_amount'] = $item['price'].'/'.$item['unit'];
                }

            }
        }
        return $listData;
    }

    /**
     *
     * 课程报表记录
     */
    public function getEliminationLists($param,$campus_id){
        $GradesDetailsModel = new GradesDetailsModel();  // 班级
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = ['status' => 1,'campus_id' => $campus_id];  //已上课
        $where_time = ['status' => 1,'campus_id' => $campus_id]; //已上课
        $where_time_arrear =  ['status' => 1,'campus_id' => $campus_id]; //已上课
        $where_sql = ['status' => 1,'campus_id' => $campus_id]; //查询条件
        if (isset($param['course_name']) && '' !== $param['course_name']) {  //课程名字
            $where['course_name'] = ['like', '%'.$param['course_name'].'%'];
        }

        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where_time['class_date'] = array('between', array($param['start_date'],$param['end_date']));
            $where_time_arrear['class_date'] = array('between', array($param['start_date'],$param['end_date']));
            $where_sql['class_date'] = array('between', array($param['start_date'],$param['end_date']));
        }



        //获取课程列表
        if(get_current_type() == 5){
            if(get_current_group_type() == 2){  //老师类
                $grades_res = $GradesDetailsModel->where(['user_teacher_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_time['class_manage_id'] = ['in',$grades_res_str];
                $where_time_arrear['class_manage_id'] = ['in',$grades_res_str];
                $where_sql['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 3){  //助教类
                $grades_res = $GradesDetailsModel->where(['user_assistant_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_time['class_manage_id'] = ['in',$grades_res_str];
                $where_time_arrear['class_manage_id'] = ['in',$grades_res_str];
                $where_sql['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 4){ //班主任
                $grades_res = $GradesDetailsModel->where(['user_master_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_time['class_manage_id'] = ['in',$grades_res_str];
                $where_time_arrear['class_manage_id'] = ['in',$grades_res_str];
                $where_sql['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 5){ //教务类
                $grades_res = $GradesDetailsModel->where(['user_edu_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where_time['class_manage_id'] = ['in',$grades_res_str];
                $where_time_arrear['class_manage_id'] = ['in',$grades_res_str];
                $where_sql['class_manage_id'] = ['in',$grades_res_str];
            }
        }

        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        $StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();
        $elimination_amount = 0;
        $elimination_no_amount = 0;
        $elimination_all_amount = 0;
        $elimination_sum = 0;
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item){
                $where_time['course_id'] = $item['id'];
                $where_time['surplus_class_elimination'] =  array('egt',0);  //>=0
                $where_time_arrear['course_id'] = $item['id'];
                $where_time_arrear['surplus_class_elimination'] =  array('lt',0); // <0
                $where_sql['course_id'] = $item['id'];

                $item['elimination_amount'] = (($StudentSubjectInfosClassModel->where($where_time)->sum('amount_deducted')) / 100);  //课消金额
                $elimination_amount = $elimination_amount + $item['elimination_amount'];

                $item['elimination_no_amount'] =(($StudentSubjectInfosClassModel->where($where_time_arrear)->sum('amount_deducted'))  / 100); //欠费课消金额
                $elimination_no_amount = $elimination_no_amount +  $item['elimination_no_amount'];

                $item['elimination_all_amount'] = (($StudentSubjectInfosClassModel->where($where_sql)->sum('amount_deducted'))  / 100);  //总金额
                $elimination_all_amount = $elimination_all_amount + $item['elimination_all_amount'];

                $item['elimination_sum'] = $StudentSubjectInfosClassModel->where($where_sql)->sum('eliminated_nums'); //总课时
                $elimination_sum = $elimination_sum + $item['elimination_sum'];
            }
        }

        $item_end = [];
        $item_end['course_name'] =  '合计';
        $item_end['elimination_amount'] =  $elimination_amount;
        $item_end['elimination_no_amount'] =  $elimination_no_amount;
        $item_end['elimination_all_amount'] =  $elimination_all_amount;  //总金额;
        $item_end['elimination_sum'] = $elimination_sum; //总课时;
        $listData['data'][count($listData['data'])] = $item_end;

        return $listData;
    }

}
